package com.nciae.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.nciae.po.Admin;
import com.nciae.po.User;
import com.nciae.service.IAdminService;
import io.swagger.annotations.Api;
import javafx.beans.property.ReadOnlySetProperty;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Guoliyu
 * @since 2022-03-30
 */
@RestController
@RequestMapping("/admin")
public class AdminController {

    @Autowired
    private IAdminService adminService;

    @GetMapping
    public ResponseEntity<List<Admin>> findAll(
            @RequestParam("index")int index,
            @RequestParam("capacity")int capacity,
            @RequestParam("username")String username
    ) {
        return ResponseEntity.ok(adminService.findAll(index, capacity, username));
    }


    /**
     * 管理员登录接口
     * @param username：用户名
     * @param password：密码
     * @return
     */
    @GetMapping("/login")
    public ResponseEntity<Admin> login(
            @RequestParam("username")String username,
            @RequestParam("password")String password
    ) {
        return ResponseEntity.ok(adminService.login(username, password));
    }

    @PostMapping
    public ResponseEntity<Boolean> add(@RequestBody Admin admin) {
        adminService.add(admin);
        return ResponseEntity.ok(true);
    }

    @PutMapping
    public ResponseEntity<Boolean> update(@RequestBody Admin admin) {
        adminService.update(admin);
        return ResponseEntity.ok(true);
    }

    @DeleteMapping("/{id}")
    public ResponseEntity<Boolean> delete(@PathVariable("id")Long id) {
        adminService.delete(id);
        return ResponseEntity.ok(true);
    }

}
